package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.BuildConfig;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.version.VersionVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.zhy.http.okhttp.OkHttpUtils;

import static com.lemon.smartsafety.app.EventTag.TAG_UPDATE_INFO;

/**
 * Created by Cooliean on 16/1/19.
 * 获取版本更新使用的服务
 */
public class UpdateServices extends BaseHttpServices {
    private static final String TAG = "UpdateServices";
    private LoggerHelper loggerHelper = new LoggerHelper(UpdateServices.class);


    private UpdateServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static UpdateServices init() {
        return new UpdateServices(IApplication.context());
    }
    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static UpdateServices init(Context context) {
        return new UpdateServices(context);
    }
    /**
     * 获取版本信息
     * @param versionCode
     * @return
     */
    public UpdateServices getUpdateInfo(int versionCode) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("version_code", versionCode+"");
        if (BuildConfig.DEBUG) {
            params.put("is_debug", "1");
        }else{
            params.put("is_debug", "0");
        }
        getUpdateInfo(params);
        return this;
    }


    public UpdateServices getUpdateInfo(LemonRequestParams params) {
        HandlerCallback<VersionVo> callback = new HandlerCallback<VersionVo>(TAG_UPDATE_INFO) {};
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_UPDATE_INFO_URL);
        OkHttpUtils.getInstance().post().url(serverUrl).params(params).build().execute(callback);
        return this;
    }
}
